package com.example.volleyball;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.volleyball.Tools.DBTools;
import com.example.volleyball.Tools.DataDB;
import com.example.volleyball.Tools.Tools;

import java.util.ArrayList;
import java.util.List;

public class JiluActivity extends AppCompatActivity {
    List<Tools> list = new ArrayList<>();
    private Button btn_xl1;     // 返回键
    private ListView listView;

    private DataDB db;
    private DBTools tools;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jilu);
        listView = findViewById(R.id.list);
        db = new DataDB(JiluActivity.this, "Data.db", null, 1);
        tools = new DBTools(db);
        list = tools.select();
        final AboutAdapter adapter = new AboutAdapter(this, R.layout.item_yuyue, list);
        listView.setAdapter(adapter);
        btn_xl1 = findViewById(R.id.btn_xl);
        btn_xl1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建Intent 对象
                Intent intent = new Intent(JiluActivity.this, Classification.class);
                //启动Activity
                startActivity(intent);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                AlertDialog dialog = new AlertDialog.Builder(JiluActivity.this)
                        .setTitle("取消预约")
                        .setMessage("是否取消预约")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i1) {
                                db.getWritableDatabase().delete("Data", "time = ? AND field = ? AND hour = ? AND name = ?", new String[]{list.
                                        get(i).time, list.
                                        get(i).field, list.
                                        get(i).hour, list.
                                        get(i).name});
                                list.remove(i);
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).show();
                return true;
            }
        });
    }
}
